import { requerconetnt, requerconetntAll } from '../utils/requestContent'
import AutoObj from './autoActivity'

let prosender

function senfun(api, data, id) {
    return new Promise((resolve, reject) => {
        chrome.tabs.sendMessage(id || prosender.tab.id, { method: 'dly-bgs', type: 'send_in_dly_script_content', api, data }, function (response) {
            // console.log('response', response)
            resolve(response)
        })
    })
}
const pageMeat = {
    // 检查对应页面打开状态
    check_url_open: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            // console.log('sender', sender.tab.id)
            // console.log('request', request)
            prosender = sender
            if (!request.data || !request.data.length) {
                await senfun('check_url_open', { code: 400, msg: '参数错误' })
                return sendResponse({ code: 400, msg: '参数错误' })
            }
            chrome.windows.getAll({ populate: true, windowTypes: ['normal'] }, async (windows) => {
                console.log(windows)
                const getTabInfo = request.data.map(res => {
                    const chraerTabs = {
                        url: res,
                        status: false,
                        id: 0
                    }
                    windows.forEach(wint => {
                        wint.tabs.forEach(element => {
                            if (element.url.includes(res)) {
                                chraerTabs.status = true
                                chraerTabs.id = element.id
                            }
                        });
                    })
                    return chraerTabs
                })
                await senfun('check_url_open', { code: 200, data: getTabInfo })

                return sendResponse({ code: 200, msg: '请求成功' })
            })
            return true
        }
    },
    // 检查插件安装状态
    check_install_plugin: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            prosender = sender
            const data = { code: 200, data: true }
            await senfun('check_install_plugin', data)
            return sendResponse(data)
        }
    },
    // 通知所有页面刷新数据
    dly_reload_all_page_autolist: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            requerconetntAll('', 'dly_reload_autodata').then(res => {
                // console.log('通知suoyo页面刷新数据成功22')
                sendResponse({ code: 200, msg: '通知suoyo页面刷新数据成功' })
            })
            return true
        }
    },
    // 用户已经登陆 可以继续任务
    doudian_user_loginstatus: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            // console.log('request'), request
            // prosender = sender
            // const data = { code: 200, data: true }
            // await senfun('doudian_user_loginstatus', data)
            AutoObj.initAutStatus(request)
            return sendResponse(true)
        }
    },

    // 打开页面标签
    create_url_page: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            console.log('request', request)
            prosender = sender
            if (!request.url) {
                await senfun('create_url_page', { code: 400, msg: '参数错误' })
                return sendResponse({ code: 400, msg: '参数错误' })
            }
            chrome.tabs.create({ url: request.url, active: false }, async (tab) => {
                // console.log('id', id)
                // chrome.tabs.update(id, { active: true })
                let data = {
                    code: 200, data: {
                        url: request.url,
                        id: tab.id,
                        status: true
                    }
                }
                await senfun('create_url_page', data)
                return sendResponse(data)
            })
            return true
        }
    },

    // 在对应页面植入脚本
    inject_script: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            prosender = sender
            console.log('request', request)
            if (!request.data || !request.data.tab_id || !request.data.script) {
                await senfun('inject_script', { code: 400, msg: '参数错误' })
                return sendResponse({ code: 400, msg: '参数错误' })
            }

            chrome.tabs.sendMessage(request.data.tab_id, { method: 'dly-bgs', type: 'send_in_dly_script_eval', api: 'inject_script', script: request.data.script }, function (response) {
                // console.log('response', response)
                // resolve(response)
                return sendResponse({ code: 200, msg: '成功' })
            })
            return true
        }
    },

    // 先对应页面发送消息
    send_message_for_page: {
        allow: { script: true },
        exec: (request, sender, sendResponse) => {

            sendResponse({ code: 200, msg: '请求成功' })
            chrome.windows.getAll({ populate: true, windowTypes: ['normal'] }, async (windows) => {

                windows.forEach(wint => {
                    wint.tabs.forEach(element => {
                        senfun('send_message_for_page', { code: 200, data: request.data }, element.id)
                    });
                })

                return sendResponse({ code: 200, msg: '请求成功' })
            })
            return true
        }
    },
    // 在对应页面调用插入的脚本函数
    call_script_function: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            prosender = sender
            console.log('request000', request, sender)
            if (!request.body || !request.tab_id) {
                await senfun('call_script_function', { code: 400, msg: '参数错误' })
                return sendResponse({ code: 400, msg: '参数错误' })
            }
            chrome.tabs.sendMessage(request.tab_id, { method: 'dly-bgs', type: 'send_in_dly_call_script_function', api: 'call_script_function', body: request.body, for_tab_id: prosender.tab.id, createTaskIdStr: request.createTaskIdStr || '' }, function (response) {
                console.log('response', response)
                return sendResponse({ code: 200, msg: '成功' })
            })
            // sendResponse({ code: 200, msg: '成功11' })
            return true
        }
    },
    call_script_function_callback: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            prosender = sender
            console.log('request', request)
            if (!request.data || !request.tab_id) {
                await senfun('call_script_function_callback', { code: 400, msg: '参数错误' })
                return sendResponse({ code: 400, msg: '参数错误' })
            }
            chrome.tabs.sendMessage(request.tab_id, { method: 'dly-bgs', type: 'send_in_dly_call_script_function_callback', api: 'call_script_function_callback', data: request.data, for_tab_id: prosender.tab.id, createTaskIdStr: request.createTaskIdStr || '' }, function (response) {
                console.log('response', response)
                return sendResponse({ code: 200, msg: '成功' })
            })
            // sendResponse({ code: 200, msg: '成功22' })
            return true
        }
    },
    // 关闭对应域名的所有页面
    close_all_page: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            if (!request.url) {
                await senfun('close_all_page', { code: 400, msg: '参数错误' })
                return sendResponse({ code: 400, msg: '参数错误' })
            }
            chrome.windows.getAll({ populate: true, windowTypes: ['normal'] }, async (windows) => {
                windows.forEach(wint => {
                    wint.tabs.forEach(element => {
                        if (element.url.indexOf(request.url) > -1) {
                            chrome.tabs.remove(element.id)
                        }
                    });
                })
                // return sendResponse({ code: 200, msg: '请求成功' })
                // chrome.tabs.sendMessage(request.data.tab_id, { method: 'dly-bgs', type: 'send_in_dly_script_eval', api: 'close_all_page', script: request.data.script }, function (response) {
                //     // console.log('response', response)
                //     // resolve(response)
                //     return sendResponse({ code: 200, msg: '成功' })
                // })
                senfun('close_all_page', { code: 200, msg: '请求成功' })

            })
            return true
        }
    },
    // 获取当前页面的信息

}

export default pageMeat

